#!/usr/bin/env bash
# @sys: ubuntu-server-16.04
# @date: Wed Sep  8 14:42:44 CST 2021
# @author: hflxhn
# @email: hflxhn@163.com
# @info: install lnmp
# @run: ./lnmp.sh



# install soft
apt install zip lrzsz gcc g++ make \
-y --allow-insecure-repositories && echo 'install soft ok'

# nginx install
useradd -M -s /sbin/nologin nginx && echo 'nginx user add ok'

apt  install libpcre3 libpcre3-dev openssl libssl-dev \
-y --allow-insecure-repositories && echo 'install rely ok'

./configure \
--prefix=/opt/lnmp/nginx \
--user=nginx --group=nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_random_index_module \
--with-http_sub_module \
--with-http_gzip_static_module \
--with-pcre \
--add-module=../nginx-http-flv-module-master \
&& echo 'nginx configure ok'

make -j12
make install

setfacl -m u:nginx:rwx -R /opt/lnmp/nginx
setfacl -m d:u:nginx:rwx -R /opt/lnmp/nginx

systemctl enable nginx.service --now
systemctl restart nginx.service




# mysql

tar xf mysql-5.7.30.tar.gz
tar xf mysql-boost-5.7.30.tar.gz
cd mysql-5.7.30

apt install cmake libtirpc-dev libncurses5-dev pkg-config

cmake -DCMAKE_INSTALL_PREFIX=/opt/lnmp/mysql \
-DSYSCONFDIR=/opt/lnmp/mysql/etc \
-DMYSQL_DATADIR=/opt/lnmp/mysql/data \
-DMYSQL_TCP_PORT=3306 \
-DEFAULT_CHARSET=utf8 \
-DEFAULT_COLLATION=utf8_general_ci \
-DMYSQL_UNIX_ADDR=/opt/lnmp/mysql/run/mysql.sock \
-DWITH_BOOST=./boost

make -j12
make install

useradd -M -s /sbin/nologin mysql

cd /opt/lnmp/mysql
bin/mysqld --initialize --basedir=/opt/lnmp/mysql --datadir=/opt/lnmp/mysql/data --user=mysql

mkdir etc run logs

echo '
[mysqld]
user = mysql
port = 3306
bind-address = 0.0.0.0
basedir = /opt/lnmp/mysql
datadir = /opt/lnmp/mysql/data
socket = /opt/lnmp/mysql/run/mysql.sock
pid-file = /opt/lnmp/mysql/run/mysqld.pid
character-set-server = utf8
collation-server = utf8_general_ci
max_connections = 1024
log-error = /opt/lnmp/mysql/logs/mysqld.log
default-time_zone = '+8:00'
# skip-grant-tables

[client]
socket = /opt/lnmp/mysql/run/mysql.sock
' > etc/my.cnf

setfacl -m u:mysql:rwx -R /opt/lnmp/mysql
setfacl -m d:u:mysql:rwx -R /opt/lnmp/mysql


echo '
[Unit]
Description=mysql service
After=network.target network-online.target syslog.target
Wants=network.target network-online.target

[Service]
Type=forking

User=mysql
Group=mysql
PIDFile=/opt/lnmp/mysql/run/mysqld.pid
ExecStart=/opt/lnmp/mysql/bin/mysqld --daemonize --pid-file=/opt/lnmp/mysql/run/mysqld.pid

[Install]
WantedBy=multi-user.target
' > /lib/systemd/system/mysqld.service

systemctl enable mysqld.service --now
systemctl restart mysqld.service

grant all privileges on *.* to 'mw'@'%' identified by '123' with grant option;





# php7.4
apt install libxml2-dev libxml2 libsqlite3-dev \
libcurl4-openssl-dev libpng-dev \
libjpeg-dev libfreetype6-dev

apt install autoconf automake libtool
tar xf oniguruma-6.9.4.tar.gz && cd oniguruma-6.9.4
./autogen.sh && ./configure --prefix=/usr
make -j12 && make install

./configure \
--prefix=/opt/lnmp/php7.4 \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-config-file-path=/opt/lnmp/php7.4/etc \
--disable-ipv6 \
--with-libxml-dir \
--enable-ftp \
--with-openssl \
--with-zlib \
--with-curl \
--enable-gd \
--with-jpeg \
--with-freetype \
--enable-mbstring \
--enable-sockets \
--with-xmlrpc \
--with-iconv-dir=/usr/local/libiconv \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-mysqlnd \
--enable-mysqlnd-compression-support \
--enable-static

make -j12
make install

cp php.ini-* /opt/lnmp/php7.4/etc/
cp php.ini-development /opt/lnmp/php7.4/etc/php.ini

cd /opt/lnmp/php7.4/

cp php-fpm.conf.default php-fpm.conf
cd php-fpm.d
cp www.conf.default www.conf

vi www.conf
listen = /opt/lnmp/php7.4/var/run/www.sock

setfacl -m u:nginx:rwx -R /opt/lnmp/php7.4
setfacl -m d:u:nginx:rwx -R /opt/lnmp/php7.4


vi php-fpm.conf
pid = /opt/lnmp/php7.4/var/run/php-fpm.pid



echo '
[Unit]
Description=phpfpm service
After=network.target syslog.target

[Service]
Type=forking
PIDFile=/opt/lnmp/php7.4/var/run/php-fpm.pid
ExecStart=/opt/lnmp/php7.4/sbin/php-fpm
ExecReload=/bin/kill -USR2 $MAINPID

[Install]
WantedBy=multi-user.target
' > /lib/systemd/system/phpfpm.service

systemctl enable phpfpm.service --now

